#include "SContentTextItem.h"
#include "Widgets/Text/STextBlock.h"
#include "EditorStyleSet.h"
#include "SuperClipBoardApp.h"



FReply SContentTextItem::DoClickItem(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent)
{
	if (SelectBorder->GetColorAndOpacity() == FLinearColor::Green)
	{
		SelectBorder->SetColorAndOpacity(DefaultColor);
		OnSelectionChanged.Execute(false,this,TEXT(""));
	}
	else
	{
		SelectBorder->SetColorAndOpacity(FLinearColor::Green);
		OnSelectionChanged.Execute(true,this,ContentText.Get());
	}
	return FReply::Handled();
}

void SContentTextItem::SetDefaultColor()
{
	SelectBorder->SetColorAndOpacity(DefaultColor);
}

const FLinearColor SContentTextItem::GetItemColor()
{
	return SelectBorder->GetColorAndOpacity();
}

void SContentTextItem::UpdateInfo()
{
	auto CalTimeText = [this]() {

		double DeltaTime = FPlatformTime::Seconds() - StartTime;
		FString TimeText;
		if (DeltaTime < 60.f)
		{
			TimeText = FString::Printf(TEXT("%d 秒前"), (int)DeltaTime);
		}
		else if (DeltaTime < 3600.f)
		{
			TimeText = FString::Printf(TEXT("%d 分钟前"), (int)DeltaTime / 60);
		}
		else if (DeltaTime < 86400)
		{
			TimeText = FString::Printf(TEXT("%d 小时前"), (int)DeltaTime / 3600);
		}
		else
		{
			TimeText = FString::Printf(TEXT("%d 天前"), (int)DeltaTime / 86400);
		}
		return TimeText;
	};
	Index++;

	TimeTextWidget->SetText(FText::FromString(CalTimeText()));
	ContentIndexWidget->SetText(FText::AsNumber(Index));
}

void SContentTextItem::Construct(const FArguments& InArgs)
{
	FSuperClipBoardApplication::InsertNewItem.AddRaw(this, &SContentTextItem::UpdateInfo);
	StartTime = FPlatformTime::Seconds();
	ContentText = InArgs._ContentText;
	
	TimeTextWidget = SNew(STextBlock).Text(FText::FromString(TEXT("0 秒前"))).ColorAndOpacity(FLinearColor::Gray).ShadowOffset(FVector2D(1.f, 1.f));
	TSharedRef<STextBlock> ContentTextWidget = SNew(STextBlock)
		.Text(FText::FromString(ContentText.Get()))
		.ColorAndOpacity(FLinearColor::White)
		.ShadowOffset(FVector2D(1.f, 1.f))
		.AutoWrapText(true)
		;
	ContentIndexWidget = SNew(STextBlock).Text(FText::AsNumber(Index)).ColorAndOpacity(FLinearColor::Gray).ShadowOffset(FVector2D(1.f, 1.f));

	OnSelectionChanged = InArgs._OnSelectionChanged;

	SelectBorder =
		SNew(SBorder)
		.BorderImage(FEditorStyle::GetBrush("ToolPanel.GroupBorder"))
		//.ColorAndOpacity(FLinearColor::Yellow)
		.OnMouseButtonUp(this, &SContentTextItem::DoClickItem)
		[
			SNew(SVerticalBox)
			+ SVerticalBox::Slot()
			.FillHeight(1.0f)
			[
				SNew(SHorizontalBox)
				+ SHorizontalBox::Slot()
				.HAlign(HAlign_Left)
				.VAlign(VAlign_Center)
				.AutoWidth()
				.Padding(FMargin(5.0f, 0.0f, 5.0f, 0.0f))
				[
					TimeTextWidget.ToSharedRef()
				]

				+ SHorizontalBox::Slot()
				.HAlign(HAlign_Left)
				.VAlign(VAlign_Center)
				.FillWidth(1.0)
				[
					SNew(SBox)
					.HAlign(HAlign_Fill)
					.VAlign(VAlign_Fill)
					.MaxDesiredHeight(200.f)
					[
						ContentTextWidget
					]
				]

				+ SHorizontalBox::Slot()
				.HAlign(HAlign_Right)
				.VAlign(VAlign_Center)
				.AutoWidth()
				.Padding(FMargin(5.0f, 0.0f, 5.0f, 0.0f))
				[
					ContentIndexWidget.ToSharedRef()
				]
			]
			
			+ SVerticalBox::Slot()
			.AutoHeight()
			[
				SNew(SBorder)
				.BorderBackgroundColor(FLinearColor::White)
			]
		];
	DefaultColor = SelectBorder->GetColorAndOpacity();

	ChildSlot
	[
		SNew(SBox)
		.Visibility(EVisibility::Visible)
		.MinDesiredHeight(50.0f)
		.MaxDesiredWidth(80.f)
		[
			SelectBorder.ToSharedRef()
		]
	];
}
